<?php
namespace app\api\service;


use think\cache\driver\Redis;

class MemberService{
    public function __construct(){
        $this->url = 'https://hfzdmember.kelimx.com';
        $this->appid = 'hfzdmember';
        $this->appsecret = '25mChF8Phxad2658BzXseNWYnH5sDAAa';
        $this->getToken = "/outapi/get_token";  //获取登录token
        $this->save_store = "/outapi/foreign/save_store";//新建门店
        $this->save_staff = "/outapi/foreign/save_staff";//新建员工
        $this->update_staff = "/outapi/foreign/update_staff";//编辑员工
        $this->role_lists = "/outapi/foreign/role_lists";//获取权限
    }

    public function getToken(){
        $data['appid'] = $this->appid;
        $data['appsecret'] = $this->appsecret;
        $redis = (new Redis(config('queue.connections.redis')));
        $token = $redis->get('MemberToken');
        if($token){
            return $token;
        }else{
            $result =Request::httpRequest($this->url.$this->getToken,'POST',json_encode($data),[]);
            $result = json_decode($result,true);
            if($result['status']==200){
                $redis->set('MemberToken',$result['data']['token'],86200);
                return $result['data']['token'];
            }else{
                return $result['msg'];
            }
        }
    }
    //推送商户信息
    public function pushMerchant($data){
        $data['store_number'] = $data['positions_code'];
        $data['phone'] = $data['mobile'];
        $headers[] = "Authori-zation: Bearer ".$this->getToken();
        $result =Request::httpRequest($this->url.$this->save_store,'POST',json_encode($data),$headers);
        $result = json_decode($result,true);
        if($result['status']==200){
            return $result['data']['finish_id'];
        }else{
            return $result['msg'];
        }
    }
    /**
     * 修改商户信息
     */
    public function editMerchant($data){
        $data['store_number'] = $data['positions_code'];
        $data['phone'] = $data['mobile'];
        $headers[] = "Authori-zation: Bearer ".$this->getToken();
        $result =Request::httpRequest($this->url.$this->save_store,'POST',json_encode($data),$headers);
        $result = json_decode($result,true);
        if($result['status']==200){
            return $result['data']['finish_id'];
        }else{
            return $result['msg'];
        }
    }
    /**
     * 获取权限
     */
    public function role_lists($id=224){
        $headers[] = "Authori-zation: Bearer ".$this->getToken();
        $result =Request::httpRequest($this->url.$this->role_lists.'/'.$id,'GET',[],$headers);
        $result = json_decode($result,true);
        if($result['status']==200){
            return $result['data'];
        }else{
            return $result['msg'];
        }
    }
    /**
     * 新增员工
     */
    public function save_staff($data){
        $headers[] = "Authori-zation: Bearer ".$this->getToken();
        $result =Request::httpRequest($this->url.$this->save_staff,'POST',json_encode($data),$headers);
        $result = json_decode($result,true);
        if($result['status']==200){
            return $result['data']['finish_id'];
        }else{
            return $result['msg'];
        }
    }
    /**
     *编辑员工
     */
    public function update_staff($id,$data){
        $headers[] = "Authori-zation: Bearer ".$this->getToken();
        $result =Request::httpRequest($this->url.$this->update_staff.'/'.$id,'POST',json_encode($data),$headers);
        $result = json_decode($result,true);
        if($result['status']==200){
            return $result['data'];
        }else{
            return $result['msg'];
        }
    }
}